// Pages/search/search.js
var getlog = require("../../utils/judgelog.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datasearch:'',
    total:0,
    /** */
    toView:"view0",
    classdata: [{ 'index': '1', 'dataName': '售卖信息' }, { 'index': 2, 'dataName': '需求信息' }],
    indexclass:0,
    activeIndex: 0,
    colorArray: ['lightcoral', 'lightgreen', 'lightskyblue', 'lightsalmon', 'lightyellow', 'lightseagreen', 'lightcoral', 'lightgreen', 'lightskyblue', 'lightsalmon', 'lightyellow', 'lightseagreen', 'lightcoral', 'lightgreen', 'lightskyblue', 'lightsalmon', 'lightyellow', 'lightseagreen'], 
    dataArray: [],
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      datasearch: options.searchdata//接受到上一个页面传过来的商品id、
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  setinput:function(e){
    var that =this;
    that.setData({
      datasearch:e.detail.value
    })
  },
  tabClick: function (e) {//改变搜索内容
    console.log(e)
    var that = this;
    var index = 0;
    for (var i = 0; i < this.data.classdata.length; i++) {
      if (this.data.classdata[i].index === e.currentTarget.dataset.item.index) {
        index = i

        break
      }
    }

    that.setData({
      activeIndex: index,//设置当前索引

    })

    if (index > 0) {
      that.setData({
        toView: 'view' + (index - 1)
      })
     
    }
  },
  bindChange: function (e) {
    var that = this
    var current = e.detail.current;

    this.setData({
      activeIndex: current,
      indexclass: current,
      toView: 'view' + parseInt(current)
    });
    this.clicksearch()/**改变数据 */
  },
  getData:function(){
    var that=this

    wx.request({
      url: 'http://47.103.21.65:5645/sell/searchSellByName',
      method:"POST",
      data: {  //参数传递
        name: this.data.datasearch,
        page: 1
      },
      header: { //设置请求头
        'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，

      },

      success: function (res) {
        console.log(res.data);
        that.setData({
          dataArray: res.data.data.info,
          total: res.data.data.total
        })
        

      },
      fail: function (error) { //请求失败
        console.log(error);

      }
    })
   },

  clicksearch: function () {
    var that = this
    var api=''
    if(that.data.activeIndex==0){
      api = 'http://47.103.21.65:5645/sell/searchSellByName'
    }else{
      api ='http://47.103.21.65:5645/require/searchRequire'

    }
    wx.request({
      url: api,
      method: "POST",
      data: {  //参数传递
        name: this.data.datasearch,
        page: 1
      },
      header: { //设置请求头
        'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，

      },

      success: function (res) {
        console.log(res.data);
        that.setData({
          dataArray: res.data.data.info,
          total: res.data.data.total
        })


      },
      fail: function (error) { //请求失败
        console.log(error);

      }
    })
  },
  jumpto: function (options) {
    /**页面跳转 */
    var flag = -1;
    getlog.getloginmode(function (result) {
      flag = result;
   
    if (flag == -1 || flag == 0) {
      /**未登录 */
      wx.showToast({
        title: '请登录后查看详细信息',
        icon: 'none'
      })
    } else {
    console.log(options.currentTarget.dataset.str);
    console.log(options);
    var thingId = JSON.stringify(options.currentTarget.dataset.str);

    wx.navigateTo({
      url: '../detailPageSell/detailPageSell?thingsId=' + thingId,
    })
    }
    })
  },
  todetail: function (e) {
    var flag = -1;
    getlog.getloginmode(function (result) {
      flag = result;

      if (flag == -1 || flag == 0) {
        /**未登录 */
        wx.showToast({
          title: '请登录后查看详细信息',
          icon: 'none'
        })
      } else {
        // console.log(options.currentTarget.dataset.str);
        // console.log(options);
        // var thingId = JSON.stringify(options.currentTarget.dataset.str);

        wx.navigateTo({
          url: '../requiredetail/requirement_detail?id=' + e.currentTarget.dataset.id,
        })
      }
    })
    
  }
})
